﻿using System;
using System.Collections.Generic;
using System.Web;

using jade.core;
using jade.lang.acl;
using jade.core.behaviours;
using Newtonsoft.Json;

//using Json
namespace CMS1 {
    public class GatewayAgent : jade.wrapper.gateway.GatewayAgent {

        public static int CONF_ID = 1;
        public static string myConf = "conf_id='" + CONF_ID + "'";

        protected Medium medium;
        public override void setup() {
            base.setup();
            addBehaviour(new Receive());
        }

        public override void processCommand(Object obj) {
            //if (obj instanceof BlackBoardBean) {

            this.medium = (Medium)obj;
            ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
            msg.addReceiver(new AID(medium.RecipientAgent, AID.ISLOCALNAME));
            msg.setContent(medium.CarriedData);
            send(msg);
            //}
        }

        class Receive : CyclicBehaviour {
            MessageTemplate fromDummy = MessageTemplate.MatchSender(new AID("Dummy", AID.ISLOCALNAME));
            MessageTemplate fromScheduler = MessageTemplate.MatchSender(new AID("Scheduler", AID.ISLOCALNAME));
            public override void action() {
                Medium medium = ((GatewayAgent)myAgent).medium;
                ACLMessage msg = myAgent.receive();
                if (msg != null) {
                    if (msg.getSender().getLocalName().Equals("Dummy")) {
                        //medium.Message = msg.getContent();
                        ((GatewayAgent)myAgent).releaseCommand(medium);
                    } else if (msg.getSender().getLocalName().Equals("Scheduler")) {
                        //throw new Exception(msg.getContent());
                        HttpContext.Current.Response.Redirect("http://www.google.com/");
                        //TextBox1.Text = msg.getContent();
                    }
                } else
                    block();
            }
        }
    }
}